package io.renren.modules.device.controller;

import com.iotplatform.client.dto.*;
import com.iotplatform.client.invokeapi.PushMessageReceiver;
import io.renren.common.utils.DateUtils;
import io.renren.modules.device.entity.DeviceDataEntity;
import io.renren.modules.device.service.DeviceDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

public class IotPushMessageReceiver extends PushMessageReceiver {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    DeviceDataService deviceDataService;



    @Override
    public void handleBody(String body) {
        logger.info("========notify body json===========");
        logger.info(body);
        logger.info("====================");
    }


    //override the callback functions if needed, otherwise, you can delete them.
    @Override
    public void handleDeviceAdded(NotifyDeviceAddedDTO body) {
        logger.info("deviceAdded ==> " + body);
        //TODO deal with deviceAdded notification
    }

    @Override
    public void handleBindDevice(NotifyBindDeviceDTO body) {
        logger.info("bindDevice ==> " + body);
        //TODO deal with BindDevice notification
    }

    @Override
    public void handleDeviceInfoChanged(NotifyDeviceInfoChangedDTO body) {
        logger.info("deviceInfoChanged ==> " + body);
        //TODO deal with DeviceInfoChanged notification
    }

    @Override
    public void handleDeviceDataChanged(NotifyDeviceDataChangedDTO body) { 
        logger.info("deviceDataChanged ==> " + body);

        DeviceDataEntity entity = new DeviceDataEntity();

        String eventTime = body.getService().getEventTime();
        Date dt = DateUtils.stringToDate(eventTime, "yyyyMMdd'T'HHmmss'Z'");
        entity.setDataDate(dt);
        entity.setDataType(body.getNotifyType());
        entity.setDis1(String.valueOf(body.getService().getData().get("Dis_1")));
        entity.setDis2(String.valueOf(body.getService().getData().get("Dis_2")));
        entity.setQ1(String.valueOf(body.getService().getData().get("Ang_X")));
        entity.setQ2(String.valueOf(body.getService().getData().get("Ang_Y")));
        entity.setDeviceId(body.getDeviceId());
        entity.setJsonData(body.toString());
        entity.setBtImei( String.valueOf(body.getService().getData().get("BT_IMEI")));
        deviceDataService.insert(entity);

    }


    @Override
    public void handleDeviceDatasChanged(NotifyDeviceDatasChangedDTO body) {
        logger.info("deviceDatasChanged ==> " + body);
    }

    @Override
    public void handleServiceInfoChanged(NotifyServiceInfoChangedDTO body) {
        logger.info("serviceInfoChanged ==> " + body);
    }

    @Override
    public void handleDeviceDeleted(NotifyDeviceDeletedDTO body) {
        logger.info("deviceDeleted ==> " + body);
    }

    @Override
    public void handleMessageConfirm(NotifyMessageConfirmDTO body) {
        logger.info("messageConfirm ==> " + body);
    }

    @Override
    public void handleCommandRsp(NotifyCommandRspDTO body) {
        logger.info("commandRsp ==> " + body);
    }

    @Override
    public void handleDeviceEvent(NotifyDeviceEventDTO body) {
        logger.info("deviceEvent ==> " + body);
    }

    @Override
    public void handleDeviceModelAdded(NotifyDeviceModelAddedDTO body) {
        logger.info("deviceModelAdded ==> " + body);
    }

    @Override
    public void handleDeviceModelDeleted(NotifyDeviceModelDeletedDTO body) {
        logger.info("deviceModelDeleted ==> " + body);
    }

    @Override
    public void handleRuleEvent(NotifyRuleEventDTO body) {
        logger.info("ruleEvent ==> " + body);
    }

    @Override
    public void handleDeviceDesiredStatusChanged(NotifyDeviceDesiredStatusChangedDTO body) {
        logger.info("deviceDesiredStatusChanged ==> " + body);
    }

    @Override
    public void handleSwUpgradeStateChanged(NotifySwUpgradeStateChangedDTO body) {
        logger.info("swUpgradeStateChanged ==> " + body);
    }

    @Override
    public void handleSwUpgradeResult(NotifySwUpgradeResultDTO body) {
        logger.info("swUpgradeResult ==> " + body);
    }

    @Override
    public void handleFwUpgradeStateChanged(NotifyFwUpgradeStateChangedDTO body) {
        logger.info("fwUpgradeStateChanged ==> " + body);
    }

    @Override
    public void handleFwUpgradeResult(NotifyFwUpgradeResultDTO body) {
        logger.info("fwUpgradeResult ==> " + body);
    }

    @Override
    public void handleNBCommandStateChanged(NotifyNBCommandStatusChangedDTO body) {
        logger.info("NBCommandStateChanged ==> " + body);
    }

}
